package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IComputeService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ArticleResultListener {

    @Autowired
    private IComputeService computeService;

    @KafkaListener(topics = "article_result")
public void handleMessage(ConsumerRecord<String,String> record){
        String value = record.value();
        System.out.println("收到聚合结果为:"+value);
        //转换成对象
        ArticleStreamMessage message = JSON.parseObject(value, ArticleStreamMessage.class);
        computeService.update(message);
    }
}